<?php
namespace Home\Controller;
use Think\Controller;
class CategoryController extends Controller {
    public function cate(){
//        $id = $_GET['id'];
//        if($id){
//            $data = D('Good_cates')->where('parent_id = '.$id)->select();
//        }else {
//            $data = D('Good_cates')->where('parent_id = 0')->select();
//        }
        $data = D('Good_cates')->select();
        $list = array();
        foreach($data as $k=>$v){
            if($v['parent_id'] == 0){
                $list[$k] = $v;
            }
        }
        foreach($list as $k=>$v){
            $a = array();
            foreach($data as $key=>$value){
                if($v['id'] == $value['parent_id']){
                    $a[$key] = $value;
                }
            }
            $list[$k]['data'] = $a;
        }
        foreach($list as $k=>$v){
            foreach($v['data'] as $a=>$b){
                $c = array();
                foreach($data as $key=>$value){
                    if($b['id'] == $value['parent_id']){
                        $c[$key] = $value;
                    }
                }
//                var_dump($list[$k]['data']);die;
                $list[$k]['data'][$a]['data'] = $c;
            }
        }

        kuayu();
        echo json_encode($list);
    }
    public function cate_index(){
        $cate1 =  D('Good_cates')->where('parent_id = 0')->select();


        kuayu();
        echo json_encode($cate1);
    }
    public function region(){

        $data = D('Good_region')->select();
        kuayu();
        echo json_encode($data);
    }
    public function select(){
        $id = $_GET['cate_id'];
        $data = array();
        $where1 = array();
        $where2 = array();
        $id1 = D('Goods')->where('cate_id='.$id)->field('brand_id')->select();
        $where1['id'] = array('in',implode(',',arrToOne($id1)));
        $data['brand'] = D('Good_brand')->where($where1)->select();

        $id2 = D('Goods')->where('cate_id='.$id)->field('region_id')->select();
        $where2['id'] = array('in',implode(',',arrToOne($id2)));
        $data['region'] = D('Good_region')->where($where2)->select();

        kuayu();
        echo json_encode($data);
    }

}